﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using System.Collections.Specialized;
using Shop.Common;

namespace Shop.Implementation.Customer
{
    public class CustomerCommon
    {
        #region 判断是否登陆
        public static bool CheckLogin(out int idcus)
        {
            System.Collections.Specialized.NameValueCollection nv = System.Web.HttpContext.Current.Request.QueryString;
            string token = nv["token"].GetVal_String();
            bool bret = Implementation.Customer.CustomerCommon.check_token(token, out idcus);
            return bret;
        }

        public static bool check_token(string _token, out int idcus)
        {
            string cmdsql = "select IDCus  from CustomerLoginProcess (nolock) where cToken=@cToken ";
            DataTable dtval = Common.HandleSQL.GetData(cmdsql, null, new SqlParameter[]
            {
                new SqlParameter("@cToken",_token)
            });
            if (dtval.Rows.Count == 0)
            {
                idcus = 0;
                return false;
            }
            else
            {
                idcus = dtval.Rows[0][0].GetVal_Int();
                Common.HandleCookie.SetCookie("idcus",string.Format("{0}", idcus));
                var openid = string.Format("{0}", Common.HandleCookie.GetCookieValue("openid"));
                if (openid.Length > 0)
                {
                    string sql = "";
                    sql += string.Format(" update Customer set cWX='{0}' where id={1} ", openid, idcus);
                    sql += " DECLARE @count INT";
                    sql += string.Format(" SELECT @count=COUNT(1) FROM dbo.CustomerOpenId WHERE IDCus={0} AND openid='{1}'", idcus, openid);
                    sql += " IF(@count=0) BEGIN";
                    sql += string.Format(@" 	INSERT INTO dbo.CustomerOpenId( IDCus, openid, dDate )VALUES  ( {0}, '{1}', GETDATE() ) END", idcus, openid);
                    Common.HandleSQL.getObj(sql);
                }
                //updateToken(idcus);
                return true;
            }
        }
        public static Dictionary<string, object> NotLogin()
        {
            return Common.ReturnValue.Get("请先登录", false);
        }
        #endregion

        public static bool CheckCustomerStatus(int idcus, out string errstr)
        {
            bool bret = true;
            string cmdsql = string.Format("select cStatus,districtID from customer where id={0}", idcus);
            DataTable dtjud = HandleSQL.GetData(cmdsql);
            errstr = "";
            if (dtjud.Rows.Count > 0)
            {
                int istatus = (dtjud.Rows[0]["cStatus"]).GetVal_Int();
                switch (istatus)
                {
                    case 1:
                        bret = false;
                        errstr = "你的帐号已经被冻结，无法下单。如有疑问，请与我公司400电话联系";
                        break;
                    case -1:
                        bret = false;
                        errstr = "你的帐号尚未激活，请联系销售激活后才能下单，谢谢！";
                        break;
                    case 0:
                        errstr = "";
                        bret = true;
                        break;
                }
            }
            if (bret)
            {
                int IDDistinct = (dtjud.Rows[0]["districtID"]).GetVal_Int();
                int igrade = 0;
                if (IDDistinct != 0)
                {
                    cmdsql = "select iGrade,districtText from district where ID= " + IDDistinct.ToString();
                    dtjud = HandleSQL.GetData(cmdsql);
                    if (dtjud.Rows.Count > 0)
                    {
                        igrade = (dtjud.Rows[0][0]).GetVal_Int();
                        if (DateTime.Now.Hour <= 7)
                        {
                            string districtText = (dtjud.Rows[0]["districtText"]).GetVal_String();
                            if (districtText.IndexOf("嘉禾站") == -1 && districtText.IndexOf("新市站") == -1)
                            {
                                bret = false; errstr = "夜市暂时只配送嘉禾/新市区域，其他区域等待我公司的通知开通！";
                            }
                        }
                    }


                }
                //if (igrade < 3)
                //{ bret = false; errstr = "你的帐号的配送还没有安排好，请先与销售联系安排后才能下单！"; }
            }
            return bret;
        }

        #region 生成一定长度的随机字符串，用于客户登录
        public static string GenerateCheckCode(int codeCount)
        {
            Random rnd = new Random();
            string srval = "";
            for (int i = 0; i < codeCount; i++)
            {
                int iSeed = rnd.Next(0, 50);
                if (iSeed > 25)
                {
                    srval += iSeed.ToString().Substring(1, 1);
                }
                else
                {
                    int iZM = 65 + iSeed;
                    char cc = Convert.ToChar(iZM);
                    srval += cc.ToString();
                }
            }
            return srval;
        }
        #endregion

    }
}